package com.ksd1.notes.util;

import android.content.SharedPreferences;

import com.ksd1.notes.BaseActivity;


public class TimeTool {
    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;

    public class mTime{
        public int hour;
        public int minute;
    }

    public String formatTime(int hour, int minute){
        String ans = "";
        if(hour < 10){
            ans += "0"+hour+" : ";
        }else{
            ans += hour+ " : ";
        }
        if(minute < 10){
            ans += "0"+minute;
        }else{
            ans += minute;
        }
        return ans;
    }

    /**
     * @param content HH : MM
     * @return
     */
    public mTime rformatTime(String content){
        mTime mtime = new mTime();
        mtime.hour = Integer.parseInt(content.substring(0,2));
        mtime.minute = Integer.parseInt(content.substring(5,7));
        return mtime;
    }

    private TimeTool(){

    }

    private static TimeTool timeTool = new TimeTool();

    public static TimeTool getInstance(){
        update();
        return timeTool;
    }


    /**
     * 更新，避免拿到旧的
     */
    private static void update(){
        sharedPreferences = BaseActivity.getContext().getSharedPreferences("data", 0);
        editor = sharedPreferences.edit();
    }


    /**
     * @param flg 设置当前记时状态
     */
    public void setTiming( Boolean flg){
        if(sharedPreferences == null || editor == null){
            sharedPreferences = BaseActivity.getContext().getSharedPreferences("data", 0);
            editor = sharedPreferences.edit();
        }

        editor.putBoolean("istiming" , flg);
        editor.apply();
    }

    /**
     * @return 获取当前记时状态
     */
    public Boolean getTiming(){
        if(sharedPreferences == null){
            sharedPreferences = BaseActivity.getContext().getSharedPreferences("data", 0);
        }
        return sharedPreferences.getBoolean("istiming", false);
    }

    public void setField(String name, String value){
        if(sharedPreferences == null){
            sharedPreferences = BaseActivity.getContext().getSharedPreferences("data", 0);
        }
        editor.putString(name , value);
        editor.apply();
    }

    public String getField(String name){
        if(sharedPreferences == null){
            sharedPreferences = BaseActivity.getContext().getSharedPreferences("data", 0);
        }
        return sharedPreferences.getString(name,"为空");
    }
}
